package com.yyplatform.admin.platform.role.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yyplatform.admin.platform.role.model.entity.SysRoleEntity;

/**
 * Created on 2024-09-14
 */
public interface SysRoleRepository extends JpaRepository<SysRoleEntity, Long>,
        JpaSpecificationExecutor<SysRoleEntity> {


    @Query(
            value = "SELECT * FROM sys_role WHERE 1=1 "
                    + " AND if(:name != '' , `name` LIKE concat('%', :name, '%'), 1=1 )  "
                    + " AND if(:code != '',  `code` LIKE concat('%', :code, '%'), 1=1 ) "
                    + " AND if(:enabled != null , `enabled` = :enabled, 1=1 ) "
                    + " AND (create_time >= :begin AND create_time <= :end) ",
            countQuery = "SELECT count(id) FROM sys_role WHERE 1=1 "
                    + " AND if(:name != '' , `name` LIKE concat('%', :name, '%'), 1=1 )  "
                    + " AND if(:code != '',  `code` LIKE concat('%', :code, '%'), 1=1 )  "
                    + " AND if(:enabled != null , `enabled` = :enabled, 1=1 ) "
                    + " AND (create_time >= :begin AND create_time <= :end) ",
            nativeQuery = true)
    Page<SysRoleEntity> findPage(
            @Param("name") String name,
            @Param("code") String code,
            @Param("enabled") Boolean enabled,
            @Param("begin") Date begin,
            @Param("end") Date end,
            @Param("pageable") Pageable pageable);


}
